/**
 * 
 */
package com.hwadee.JPKC.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hwadee.JPKC.entity.courseInfo;
import com.hwadee.JPKC.entity.user;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwadee.JPKC.entity.admin;

import com.hwadee.JPKC.entity.admin;
import com.hwadee.JPKC.entity.conmment;
import com.hwadee.JPKC.entity.courseDeclare;
import com.hwadee.JPKC.entity.user;
import com.hwadee.JPKC.service.AdminService;
import com.hwadee.JPKC.service.ConmmentService;
import com.hwadee.JPKC.service.CourseInfoService;
import com.hwadee.JPKC.service.UserService;


/**
 * 
 * @ClassName: 超级管理员
 * 
 * @Description: 
 * 
 * @author hui
 * 
 * @date 2020-03-03
 * 
 * @Copyright 
 * 
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

	
	@Autowired
	private CourseInfoService courseInfoService;
	
	@Autowired
	private UserService userService;

	@Autowired
	private AdminService adminservice;
	
	@Autowired
	private ConmmentService conmmentervice;
	
	/*
	 * 登录页
	 */
	@RequestMapping("/")
	public String login() {
		
		return "admin/Adminlogin";
	}
	
	/*
	 * 登录检查
	 */
	@RequestMapping("/Adminlogin")
	public String Adminlogin(admin admin,Model model,HttpSession session) {
		
		if (StringUtils.isEmpty(admin.getAdminName()) ||StringUtils.isEmpty(admin.getAdminPw()) ) {
			model.addAttribute("errorMsg", "账号或密码不能为空");
			return "/admin/Adminlogin";
		}
		admin admins = adminservice.selectByName(admin.getAdminName());
		if (null == admins || !admins.getAdminPw().equals(admin.getAdminPw())) {
			model.addAttribute("errorMsg", "密码错误！");
			return "/admin/Adminlogin";
			
		} else {
			session.setAttribute("admins", admins);
			return "redirect:/admin/index.ht";
		}
		
	}

	/*
	 * 后台主页
	 */
	@RequestMapping("/index.ht")
	public String index(Model model) {
		
		List<user> users =userService.FindAllLimt();
		List<courseInfo> courseInfos =courseInfoService.FindAll3();
		List<courseInfo> courseInfosRe =courseInfoService.FindAllRe();
		int max=courseInfosRe.get(0).getCourseNum();
		int sum=0;
		for(int i =0;i<courseInfosRe.size();i++) {
			sum=sum+courseInfosRe.get(i).getCourseNum();
		}
		model.addAttribute("max", max);
		model.addAttribute("sum", sum);
		model.addAttribute("users", users);
		model.addAttribute("courseInfos", courseInfos);
		model.addAttribute("courseInfosRe", courseInfosRe);
		return "admin/index";
		
	}

	/*
	 * 
	 */
	@RequestMapping("/basicTable.ht")
	public String basicTable() {
		return "admin/basic-table";
	}
	/*
	 * 所有课程查看
	 */
	@RequestMapping("/courseInfo.ht")
	public String getAll(@RequestParam(name = "p", required = true, defaultValue = "1") Integer p,courseInfo courseInfo,Model model) {
		
		Page<courseInfo> course = PageHelper.startPage(p, 5).doSelectPage(()->courseInfoService.getAll());
		model.addAttribute("course", course);
	
		return "admin/fontawesome";
	}
	
	/*
	 * 评论操作
	 */
	@RequestMapping("/blank.ht")
	public String blank(@RequestParam(name = "p", required = true, defaultValue = "1") Integer p,Integer expertId,conmment conmment,Model model) {
		Page<conmment> co = PageHelper.startPage(p, 5).doSelectPage(()->conmmentervice.getAll());

		//删除评论
		conmmentervice.deleteByPrimaryKey(expertId);
		
		model.addAttribute("co",co);

		return "admin/blank";
	}
	
}
